let jweixin = require('labs/jweixin.js');

let Weixin = function () {
	this.lastUrl = "";
}

Weixin.prototype = {
	constructor: Weixin,
	ready (callback) {
		let currentUrl = location.href;
		if (currentUrl === this.lastUrl) {
			jweixin.ready(() => {
				callback(jweixin);
			})
			return;
		}
		uni.post("/api/wechat/WechatInfo", {
			api: "",
			url: currentUrl
		}, msg => {
			msg.wechatConfig.jsApiList = [
				'updateAppMessageShareData', 
				'updateTimelineShareData',
				'chooseImage',
				'previewImage',
				'uploadImage',
				'downloadImage',
				'openLocation',
				'getLocation',
				'closeWindow',
				'scanQRCode',
				'openProductSpecificView', 'openAddress']
			jweixin.config(msg.wechatConfig);
			jweixin.ready(() => {
				this.lastUrl = currentUrl;
				callback(jweixin);
			})
		});
	}
}

let rwx = new Weixin();
module.exports = rwx;